<?php

namespace App\Services;

use App\Enums\SmsTempEnum;
use App\Models\Goods;
use App\Models\Order;
use App\Models\OrderGoods;
use App\Models\OrderLease;
use App\Models\OrderReturn;
use App\Models\SmsRecord;
use App\Models\SmsTemplate;
use Illuminate\Support\Facades\Log;

class ChargeService
{

    /**
     * @param $mobile
     * @param $message
     * @param $sms_template_scene
     * @param string $order_id
     * @param string $lease_id
     *
     */
    public static function smsUseConfigTemplate($sms_template_scene, string $order_id='', string $lease_id='')
    {
                    $sms_template=SmsTemplate::query()->where('scene',$sms_template_scene)->select('sms_content','id')->first();
                    $message=$sms_template['sms_content'];
                    $sms_template_id=$sms_template['id'];
                    $mobile='';
                    if($order_id){
                        $order=Order::query()->from('sp_order_2 as o')
                            ->join('sp_member_2 as m','o.uid','=','m.id')
                            ->where('o.id',$order_id)->select('m.mobile','o.status','o.withhold_status','o.sign_status')->first();
                        $mobile=$order['mobile'];

                        if($sms_template_scene==SmsTempEnum::PENDING_PAYMENT){
                            if($order['status']!=OrderService::ORDER_STATUS_SIGN_NOTPAY){
                                Log::info($order_id.'订单不是待支付状态，不能发送短信');
                                return true;
                            }

                            if($order['withhold_status']!=1||$order['sign_status']!=2){
                                Log::info($order_id.'订单合同未签约，不能发送短信');
                                return true;
                            }

                            $rent=OrderLease::query()->where('order_id',$order_id)->where('which_period',1)->value('rent');
                            $message=str_replace('{price}',$rent,$message);

                        }

                        if($sms_template_scene==SmsTempEnum::CONFIRM_ORDER_INFO&&$order['status']!=OrderService::ORDER_STATUS_SIGN_NOTPAY){
                            Log::info($order_id.'订单不是待支付状态2，不能发送短信');
                            return true;
                        }
                        if($sms_template_scene==SmsTempEnum::CONFIRM_ORDER_INFO&&$order['withhold_status']==1&&$order['sign_status']==2){
                            Log::info($order_id.'订单已签约');
                            return true;
                        }
                        $goods=OrderGoods::query()->where('order_id',$order_id)->select('goods_name','goods_title','goods_id')->first();
                        $new_grade=Goods::query()->where('id',$goods['goods_id'])->value('new_grade');
                        $new_grade_dec=GoodsService::getGoodsNewGrade($new_grade);
                        $goodsName=$goods['goods_name']??$goods['goods_title'];
                        $message=str_replace('{goods_name}',$new_grade_dec.$goodsName,$message);


                    }
                    if(!$mobile){
                        Log::error("法诉短信发送失败，原因：手机号码不存在");
                    }



                   if($lease_id){
                        $which_period=OrderLease::query()->where('id',$lease_id)->value('which_period');
                        $message=str_replace('{number}',$which_period,$message);
                    }
                    if($sms_template_scene=='shipped_notification'){
                        $express_no=OrderReturn::query()->where('order_id',$order_id)->value('logistics_no');
                        $message=str_replace('{express_no}',$express_no,$message);
                    }

                    $send_res = SmsService::send_msg($mobile,$message);
                    if($send_res['status']){
                        $send_status = 1;
                        $reason = 'OK';
                    }else{
                        $send_status = 2;
                        $reason = $send_res['msg'];
                    }
                    $sms_content = $message;
                    $data = array();
                    $data['order_id'] = $order_id;
                    $data['lease_id'] = $lease_id;
                    $data['mobile'] = $mobile;
                    $data['template_id'] = $sms_template_id;
                    $data['content'] = $sms_content;
                    $data['send_user'] = 1;
                    $data['send_status'] = $send_status;
                    $data['reason'] = $reason;
                    $data['create_time'] = date('Y-m-d H:i:s');
                    $add_res = SmsRecord::query()->insert($data);
                    if($send_res['status'] && $add_res){ //发送成功
                        return ['status'=>CommonService::HTTP_CODE_SUCCESS,'msg'=>'发送成功'];
                    }else{
                        return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'发送失败','data'=>$reason];
                    }

    }





}
